﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Reporting.WinForms;
using System.IO;
using DBS;
using System.Data;
using Ais3.DBS.Requirement.BE;
using Oracle.DataAccess.Client;
using Ais3.DBS.Requirement.DALC;

namespace Reporting
{
	public class Requirement
	{

		public void CreateReport()
		{
			RequirementDoc doc;
			using (RequirementDocDA dalc = new RequirementDocDA())
			{
				doc = dalc.GetById(2443306);
			}

			ReportRequirementDoc rdoc = new ReportRequirementDoc(doc);

			rdoc.TaxOrganName = "Название налоговой";
			rdoc.TaxOrganAddress = "Адрес налоговой";

			LocalReport rpt = new LocalReport();
			using (Stream embed = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Reporting.Reports.RequirementFL.rdlc"))
				rpt.LoadReportDefinition(embed);

			ReportDataSourceCollection rds = rpt.DataSources;
			rds.Add(new ReportDataSource("RequirementDocLine", doc.DocLines));
			rds.Add(new ReportDataSource("ReportRequirementDoc", new List<ReportRequirementDoc>() { rdoc }));

			byte[] bytes = rpt.Render("PDF");

			using (FileStream stream = new FileStream(@"..\..\..\Reporting\Sandbox\test.pdf", FileMode.Create))
				stream.Write(bytes, 0, bytes.Length);

		}
		public void CreateReport1()
		{
			DataTable table;
			using (BaseDALC dalc = new BaseDALC())
			{
				table = dalc.Fill("select id from DBS$MON.req where rownum<10");
			}
			List<decimal> ids = new List<decimal>();
			foreach (DataRow row in table.Rows)
				ids.Add((decimal)row["id"]);
			ids.Add(2443225);
			ids.Add(2443081);
			ids.Add(2443306);

			DataTable reqs;
			DataTable debts;
			DataTable pds;
			using (BaseDALC dalc = new BaseDALC())
			{
				reqs = dalc.Fill(
					@"select req.*,
						case when specifying_req_sign = 1 then (select doc_num from DBS$MON.req req1 where req1.specifying_req_id = req.id) else null end spec_doc_num,
						case when specifying_req_sign = 1 then (select doc_date from DBS$MON.req req1 where req1.specifying_req_id = req.id) else null end spec_doc_date
						from DBS$MON.req
						where id in (" + string.Join(",", ids.ToArray()) + ")");
			}


			LocalReport rpt = new LocalReport();
			using (Stream embed = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Reporting.Reports.RequirementDataset.rdlc"))
				rpt.LoadReportDefinition(embed);

			ReportDataSourceCollection rds = rpt.DataSources;
			rds.Add(new ReportDataSource("REQ", reqs));

			byte[] bytes =  rpt.Render("PDF");

			using (FileStream stream = new FileStream(@"..\..\..\Reporting\Sandbox\test.pdf", FileMode.Create))
				stream.Write(bytes, 0, bytes.Length);

		}
	}
}
